<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Basc
{
    protected $appends = ["action","pic_url"];
    public function getActionAttribute(){
        $html = $this->editBtn("admin.article.edit","修改",$this);
        $html .= " " . $this->deleteBtn("admin.article.destroy","删除",$this);
        return $html;
    }
    public function getPicUrlAttribute(){
        return $this->domain.$this->pic;
    }
    public function getBodyAttribute(){
        $preg = "#src'(.*)'#iUs";
        # 获取对应模型的数据
        $body = $this->attributes["body"];
        # 将转义的斜杠去除
        $html = str_replace("\\","",$body);
        $domain = $this->domain;
        # 正则替换
        $html = preg_replace($preg,function($match) use ($domain){
            if(strstr($match[1],"http://")){
                return "src='".$match[1] . "'";
            }else{
                return "src='".$domain . $match[1] . "'";
            }
        },$html);
        return $html;
    }
}
